In [9]:
    
%matplotlib inline
import numpy as np
import matplotlib.cm as cm
from matplotlib import pyplot as plt
plt.style.use('ggplot')
    
In [10]:
    
x=[]
yt=[]
for i in range(20):
    x.append( [0.5+np.random.rand(), 0.5+np.random.rand()])
    yt.append([0 , 1])
for i in range(20):
    x.append( [-0.5+np.random.rand(), -0.5+np.random.rand()])
    yt.append([1 , 0])
    
x=np.array(x)
yt=np.array(yt)
    
In [11]:
    
plt.plot( x[yt[:,1]==1,0], x[yt[:,1]==1,1], 'ob')
plt.plot( x[yt[:,0]==1,0], x[yt[:,0]==1,1], 'or')
    
    Out[11]:
    
$ \begin{eqnarray} \Delta w_0 &=& -\eta \frac{\partial E}{\partial w_0}\\ \frac{\partial E}{\partial w_0} &=& \frac{\partial E}{\partial y_p} \frac{\partial y_p}{\partial a_1} \frac{\partial a_1}{\partial z_0} \frac{\partial z_0}{\partial w_0} \\ \frac{\partial E}{\partial y_p} &=& y_p-y_t \\ \frac{\partial y_p}{\partial a_1} &=& 1 \\ \frac{\partial a_1}{\partial z_0} &=& \frac{\partial g(z_0)}{\partial z_0} \\ \frac{\partial z_0}{\partial w_0} &=& a_0 \\ \end{eqnarray} $
$ \begin{eqnarray} \Delta b_0 &=& -\eta \frac{\partial E}{\partial b_0}\\ \frac{\partial E}{\partial w_0} &=& \frac{\partial E}{\partial y_p} \frac{\partial y_p}{\partial a_1} \frac{\partial a_1}{\partial z_0} \frac{\partial z_0}{\partial b_0} \\ \frac{\partial E}{\partial y_p} &=& y_p-y_t \\ \frac{\partial y_p}{\partial a_1} &=& 1 \\ \frac{\partial a_1}{\partial z_0} &=& \frac{\partial g(z_0)}{\partial z_0} \\ \frac{\partial z_0}{\partial b_0} &=& 1 \\ \end{eqnarray} $
In [12]:
    
def g(x):    
    return 1/(1+np.exp(-x))
def grad_g(g):
        return (1-g)*g
# def g(x):    
#     return np.tanh(x)
# def grad_g(g):
#         return 1-g*g
    
In [13]:
    
#random init weight and bias
np.random.seed(1)
a0=np.concatenate((x, np.ones([x.shape[0],1])), axis=1)
w0 = np.random.random((2,2)) # [3x2] 2 inputs x 2 nodes
b0 = np.random.random((1,2)) # [3x2] 1 bias x 2 nodes
wb0=np.concatenate((w0, b0), axis=0)
for i in range(100):
    #forward x=a0, a1=yp
    z0=np.dot(a0,wb0)
    a1=g(z0) 
    #backward
#     d_a1=yt-a1
#     d_z0=d_a1*grad_g(a1)
#     d_wb0 = np.dot(a0.T,d_z0)
    
    # Exam
    yp=a1
    d_wb0=-0.1*np.dot(a0.T,(yp-yt)*(1-yp)*yp)
    
    wb0 += d_wb0
    if(i % 10) == 0:   # Only print the error every 10000 steps
        E=0.5*np.sum(np.square(yp-yt))
        print("Error: {}".format(E))
    
    
In [14]:
    
a0.shape
    
    Out[14]:
In [15]:
    
wb0.shape
    
    Out[15]:
In [16]:
    
x.shape
    
    Out[16]:
In [17]:
    
my,mx=np.mgrid[slice(-1,2,0.01),slice(-1,2,0.01)]
    
In [18]:
    
out=np.zeros(mx.shape)
for i in range(mx.shape[0]):
    for j in range(mx.shape[1]):
        u=[ mx[i,j], my[i,j],1]
        #forward
        hot=g(np.dot(u,wb0))
        out[i,j]=hot[1]-hot[0]
plt.pcolor(mx,my,out,cmap=cm.RdYlBu)
plt.colorbar()
plt.plot( x[yt[:,1]==1,0], x[yt[:,1]==1,1], 'ob')
plt.plot( x[yt[:,0]==1,0], x[yt[:,0]==1,1], 'or')
    
    Out[18]:
    
In [ ]: